<template>
  <div class="login-container">
    <div class="login-form">
      <h2>钛考考试系统</h2>
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" @submit.prevent="handleLogin">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入学号或手机号" prefix-icon="User" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" show-password prefix-icon="Lock" />
        </el-form-item>

        <el-form-item prop="captcha">
          <div class="captcha-input-container">
            <el-input v-model="loginForm.captcha" placeholder="请输入验证码" prefix-icon="Lock">
              <template #suffix>
                <el-image :src="captchaImage" @click="refreshCaptcha" class="captcha-img" title="点击刷新" />
              </template>
            </el-input>
          </div>
        </el-form-item>

        <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin" style="width: 100%">
          {{ loading ? '登录中...' : '登录' }}
        </el-button>

        <div class="links-container">
          <div class="forgot-link">
            <router-link to="/forgot-password">忘记密码？</router-link>
          </div>

          <div class="register-link">
            还没有账号？<router-link to="/register">立即注册</router-link>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/auth'
import { ElMessage } from 'element-plus'
import type { FormRules, FormInstance } from 'element-plus'

// 使用Element Plus表单引用
const loginFormRef = ref<FormInstance>()

interface RuleForm {
  username: string,
  password: string,
  captcha: string
}
// 路由和用户状态管理
const router = useRouter()
const userStore = useUserStore()

// 表单数据
const loginForm:RuleForm = reactive({
  username: '',
  password: '',
  captcha: ''
})

// 表单验证规则
const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: '请输入学号或手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 4, max: 4, message: '验证码为4位字符', trigger: 'blur' }
  ]
})

// 验证码图片
const captchaImage = ref("http://localhost:8080/captcha/captchaImage");

// 加载状态
const loading = ref(false)

// 刷新验证码
const refreshCaptcha = () => {
  const baseUrl = "http://localhost:8080/captcha/captchaImage";
  captchaImage.value = `${baseUrl}?t=${Date.now()}`;
}

// 处理登录
const handleLogin =  () => {
  loginFormRef.value?.validate(
        async(valid:boolean)=>{
        if(valid){
            //校验通过
           await userStore.login(loginForm)
            router.push('/123')
        }
    })
}

// 回车登录功能
// const handleKeyDown = (event) => {
//   if (event.key === 'Enter') {
//     handleLogin()
//   }
// }

</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-form {
  background: white;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  width: 400px;
}

.login-form h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.captcha-input-container {
  display: flex;
  gap: 10px;
}

.captcha-img {
  width: 120px;
  height: 40px;
  cursor: pointer;
  border-radius: 4px;
}

.links-container {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.forgot-link,
.register-link {
  font-size: 14px;
}

.forgot-link a,
.register-link a {
  color: #409eff;
  text-decoration: none;
}

.forgot-link a:hover,
.register-link a:hover {
  text-decoration: underline;
}
</style>